<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Paper extends Model
{
    protected $fillable = ['name', 'duration', 'type', 'teacher_id', 'status'];

    // 关联所属教师
    public function teacher()
    {
        return $this->belongsTo(Admin::class, 'teacher_id');
    }

    // 关联题目
    public function questions()
    {
        return $this->belongsToMany(Question::class, 'paper_questions');
    }

    // 关联课程（多对多）
    public function courses()
    {
        return $this->belongsToMany(Course::class, 'course_papers');
    }

    // 获取题目数量
    public function getQuestionCountAttribute()
    {
        return $this->questions()->count();
    }
}